package praktikumid.p03_t;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Kolmas praks.
 * @author ago
 *
 */
public class Kolmas {

	/**
	 * main.
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length > 0) {
			System.out.println(args[0]);
			String word = args[0];
		} else {
			System.out.println("ei saanud argumenti");
		}
		try {
			System.out.println(args[10]);
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("ei saanud 10. argumenti");
		}
		/*
		System.out.println(readLine());
		//System.out.println(args[1]);
		
		try {
			String a = readLine2();
			System.out.println(a);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("ei saanud lugeda");
		}*/
		
		System.out.println("siseta taisarv:");
		int i = readInt();
		System.out.println("sain arvu: " + i);
		
		System.out.println("siseta taisarv:");
		int j = 0;
		try {
			j = readInt2();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("viga sisendiga");
			System.exit(0);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			j = 0;
			System.out.println("ei saanud taisarvu, kasutan 0");
		}
		System.out.println("sain arvu: " + j);
		
		
		while (true) {
			try {
				j = readInt2();
				// kui number korras, siis breigime while'ist valja
				break;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				j = 0;
				System.out.println("sisendi probleem");
				System.exit(0);
			} catch (NumberFormatException e) {
				System.out.println("ei ole arv");				
			}
		}
		
		System.out.println("sain arvu " + j);
		
	}
	
	/**
	 * Loeb rea standardsisendist ja tagastab selle.
	 * @return String
	 */
	public static String readLine() {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		String line;
		try {
			// koodi kaivitatakse
			line = br.readLine();
			return line;
			// seda ei kaivitata
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			return null;
		}
	}
	
	/**
	 * Loeb sisendit. Viskab (voib visata) exceptioni.
	 * @return String
	 * @throws IOException
	 */
	public static String readLine2() throws IOException {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		String line;
		line = br.readLine();
		return line;
	}
	
	/**
	 * Loeb taisarvu. Kui sisestatakse midagi muud, tagastab 0.
	 * @return taisarv
	 */
	public static int readInt() {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		String line;
		try {
			line = br.readLine();
			//
			return Integer.parseInt(line);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("ei ole sisendit");
		} catch (NumberFormatException e) {
			System.out.println("ei ole number, tagastan 0");
			return 0;
		}
		return 0;
	}
	
	/**
	 * Loeb taisarvu.
	 * @return taisarv
	 * @throws IOException
	 * @throws NumberFormatException
	 */
	public static int readInt2() throws IOException, NumberFormatException {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		String line;
		line = br.readLine();
		//throw new NumberFormatException("mingi tekst");
		return Integer.parseInt(line);
	}
	
}
